﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using IView.DiaryAndTracking;
using Presenter.DiaryAndTracking;
using DataTransferObject.DiaryAndTrackingTask.SupportClassForDiary;
using DataTransferObject.DiaryAndTrackingTask.SupportClassForUpdateTask;

namespace JSKE.DiaryAndTracking
{
    public partial class DiaryAnalyzeForm : FormLang, IDiaryAnalyzeForm
    {
        PresenterDiaryAnalyzeForm _Presenter;

        private const int PRIORITY_CHART = 1;
        private const int EVENTYPE_CHART = 2;
        private const int SATISFY_CHART = 3;

        public DiaryAnalyzeForm()
        {
            //InitializeComponent();
            CreateTableColor();
            _Presenter = new PresenterDiaryAnalyzeForm(this, DATA_MODE);
            cbbViewEventTime.SelectedIndex = 2;
            _Presenter.createAnalyzeReport(dtpFromDay.Value, dtpToDay.Value);
        }
        public DiaryAnalyzeForm(String message)
        {
            //InitializeComponent();
        }

        public DiaryAnalyzeForm(List<JskeEvent> jskeEvents, DateTime from, DateTime to)
        {
            // TODO: Complete member initialization
            //InitializeComponent();
            CreateTableColor();
            cbbViewEventTime.SelectedIndex = 4;
            //this._jskeEvents = jskeEvents;
            dtpFromDay.Value = from;
            dtpToDay.Value = to;

            _Presenter = new PresenterDiaryAnalyzeForm(this, jskeEvents, DATA_MODE);
        }

        public DiaryAnalyzeForm(DateTime from, DateTime to)
        {
            // TODO: Complete member initialization
            //InitializeComponent();
            CreateTableColor();

            //this._jskeEvents = jskeEvents;
            dtpFromDay.Value = from;
            dtpToDay.Value = to;
            cbbViewEventTime.SelectedIndex = 4;

            _Presenter = new PresenterDiaryAnalyzeForm(this, DATA_MODE);
            _Presenter.createAnalyzeReport(dtpFromDay.Value, dtpToDay.Value);
        }

        private void btnAnalyze_Click(object sender, EventArgs e)
        {
            if (dtpFromDay.Value >= dtpToDay.Value)
            {
                MessageBox.Show("Invalid time!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                _Presenter.createAnalyzeReport(dtpFromDay.Value, dtpToDay.Value);
            }
        }
        List<Color> tableColor;
        private void CreateTableColor()
        {
            tableColor = new List<Color>(16);
            tableColor.Add(Color.FromArgb(0, 127, 0));
            tableColor.Add(Color.FromArgb(255, 0, 0));
            tableColor.Add(Color.FromArgb(127, 127, 0));
            tableColor.Add(Color.FromArgb(255, 127, 0));
            tableColor.Add(Color.FromArgb(0, 255, 0));
            tableColor.Add(Color.FromArgb(0, 0, 127));
            tableColor.Add(Color.FromArgb(127, 0, 127));
            tableColor.Add(Color.FromArgb(127, 127, 127));
            tableColor.Add(Color.FromArgb(127, 0, 0));
            tableColor.Add(Color.FromArgb(255, 127, 127));
            tableColor.Add(Color.FromArgb(255, 255, 127));
            tableColor.Add(Color.FromArgb(0, 0, 255));
            tableColor.Add(Color.FromArgb(127, 0, 255));
            tableColor.Add(Color.FromArgb(0, 255, 255));
            tableColor.Add(Color.FromArgb(255, 0, 255));
            tableColor.Add(Color.FromArgb(0, 127, 255));
        }
        
        #region IDiaryAnalyzeForm Members

        /// <summary>
        /// Được Presenter gọi để vẽ đồ thị lên Form sau khi đã lấy được dữ liệu Analysis hợp lệ
        /// </summary>
        /// <param name="analysis"></param>
        public void createAnalysisGraph(AnalysisDS analysis)
        {
            if (optEvent.Checked)
                drawPieChart(analysis, EVENTYPE_CHART);
            else if (optPriority.Checked)
                drawPieChart(analysis, PRIORITY_CHART);
            else if (optEventSatisfy.Checked)
                drawPieChart(analysis, SATISFY_CHART);
        }
        private void drawPieChart(AnalysisDS analysis, int pieChartType)
        {
            #region Sort lai Analysis theo thu tu tu lon den be
            analysis.StatisticEventType.ListStatisticData.Sort(delegate(StatisticItem s1, StatisticItem s2) {
                return s2.Number.CompareTo(s1.Number);
            });

            analysis.StatisticPriority.ListStatisticData.Sort(delegate(StatisticItem s1, StatisticItem s2)
            {
                return s2.Number.CompareTo(s1.Number);
            });

            #endregion
  

            int numberOfItem = 0;
            List<PiechartItem> data = new List<PiechartItem>();
            if (pieChartType == PRIORITY_CHART)
            {                
                numberOfItem = analysis.StatisticPriority.ListStatisticData.Count;
                for (int i = 0; i < numberOfItem; i++)
                {
                    data.Add(new PiechartItem(analysis.StatisticPriority.ListStatisticData[i].Number, analysis.StatisticPriority.ListStatisticData[i].Name));
                }
                jSkePieChart1.setData(data);
                jSkePieChart1.setChart3DStyle(false);
                jSkePieChart1.setTransperency3D(false);
                jSkePieChart1.setPriorityHeaderText("Priority");
                jSkePieChart1.setMessage(analysis.EventPriorityConclusion);
                jSkePieChart1.setChartLablePercentLegendtxt(true);
            }
            else if (pieChartType == EVENTYPE_CHART)
            {
                numberOfItem = analysis.StatisticEventType.ListStatisticData.Count;
                for (int i = 0; i < numberOfItem; i++)
                {
                    data.Add(new PiechartItem(analysis.StatisticEventType.ListStatisticData[i].Number, analysis.StatisticEventType.ListStatisticData[i].Name));
                }
                jSkePieChart1.setData(data);
                jSkePieChart1.setChart3DStyle(false);
                jSkePieChart1.setTransperency3D(false);
                jSkePieChart1.setPriorityHeaderText("EventType");
                jSkePieChart1.setMessage(analysis.EventTypeConclusion);
                jSkePieChart1.setChartLablePercentLegendtxt(false);
            }
            else if (pieChartType == SATISFY_CHART)
            {
                numberOfItem = analysis.StatisticEventSatisfy.ListStatisticData.Count;
                for (int i = 0; i < numberOfItem; i++)
                {
                    data.Add(new PiechartItem(analysis.StatisticEventSatisfy.ListStatisticData[i].Number, analysis.StatisticEventSatisfy.ListStatisticData[i].Name));
                }
                jSkePieChart1.setData(data);
                jSkePieChart1.setChart3DStyle(true);
                jSkePieChart1.setTransperency3D(true);
                jSkePieChart1.setPriorityHeaderText("Result");
                jSkePieChart1.setMessage(analysis.EventSatisfyConclusion);
                jSkePieChart1.setChartLablePercentLegendtxt(true);
            }
            else
            {
                MessageBox.Show("You must specify a Piechart Type", "Notice",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }
        #endregion

        private void cbbViewEventTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            dtpToDay.Value = DateTime.Today;
            int timeSpan = cbbViewEventTime.SelectedIndex;

            switch (timeSpan)
            {
                case 0:
                    dtpFromDay.Value = DateTime.Today.AddDays(-1.0);
                    break;
                case 1:
                    dtpFromDay.Value = DateTime.Today.AddDays(-7.0);
                    break;
                case 2:
                    dtpFromDay.Value = DateTime.Today.AddDays(-30.0);
                    break;
                case 3:
                    dtpFromDay.Value = dtpFromDay.MinDate;
                    break;
                default:
                    break;
            }
            dtpFromDay.Enabled = dtpToDay.Enabled = cbbViewEventTime.Text == "Custom";
        }

        private void optPriority_CheckedChanged(object sender, EventArgs e)
        {
            createAnalysisGraph(_Presenter.AnalysisData);
        }

        private void optEvent_CheckedChanged(object sender, EventArgs e)
        {
            createAnalysisGraph(_Presenter.AnalysisData);
        }

        private void optEventSatisfy_CheckedChanged(object sender, EventArgs e)
        {
            createAnalysisGraph(_Presenter.AnalysisData);
        }

        
    }
}
